using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using objetos;
using dados;
using System.IO;
using publico;

namespace negocios
{
	#region TB_ORDER_BOOK_TRUCK
	/// <summary>
	/// This object represents the properties and methods of a TB_ORDER_BOOK_TRUCK.
	/// </summary>
	public class BLL_TB_ORDER_BOOK_TRUCK
	{		
		DAL_TB_ORDER_BOOK_TRUCK DAL = new DAL_TB_ORDER_BOOK_TRUCK();
		
		#region METODOS PROCEDURES
		/// <summary>
		/// Incluir via procedure
		/// </summary>
		public int Incluir(OBJ_TB_ORDER_BOOK_TRUCK obj)
		{
			return	DAL.Incluir(obj);
			
		}
		
		/// <summary>
		/// Alterar via procedure
		/// </summary>
		public void Alterar(OBJ_TB_ORDER_BOOK_TRUCK obj)
		{
			DAL.Alterar(obj);	
		}
		
		/// <summary>
		/// Excluir via procedure
		/// </summary>
		public void Excluir(OBJ_TB_ORDER_BOOK_TRUCK obj)
		{
			DAL.Excluir(obj);	
		}
		
		/// <summary>
		/// Excluir Lista via procedure
		/// </summary>
		public void ExcluirLista(List<OBJ_TB_ORDER_BOOK_TRUCK> lista)
		{
				foreach (OBJ_TB_ORDER_BOOK_TRUCK obj in lista)
            	{
                this.Excluir(obj);
            	}
			
		}
						
		/// <summary>
		/// Pesquisar Parametros Where, OrderBy, PageIndex, PageSize
		/// </summary>		
		public DataSet Selecionar(string Where, string OrderBy, int PageIndex, int PageSize)
		{
			
			return DAL.Selecionar(Where,OrderBy,PageIndex,PageSize);
			
		}		
		
		
		/// <summary>
		/// Retorna Objeto por ID
		/// </summary>
		public OBJ_TB_ORDER_BOOK_TRUCK SelecionarObjetoPorId(int id)
		{
			return DAL.SelecionarObjetoPorId(id);
			
		}
				
			
		/// <summary>
		/// Retorna DataSet Parametros Where, OrderBy
		/// </summary>	
		public DataSet Selecionar(String Where, String OrderBy)
        {
            return DAL.Selecionar(Where, OrderBy);
        }
		
		/// <summary>
		/// Retorna DataSet
		/// </summary>	
		public DataSet Selecionar()
        {
            return DAL.Selecionar();
        }
			
		
		/// <summary>
		/// Retorna uma lista de objetos por parametro Where, OrderBy
		/// </summary>
		public List<OBJ_TB_ORDER_BOOK_TRUCK>SelecionarLista(String Where, String OrderBy)
		{						
			return 	DAL.SelecionarLista(Where,OrderBy);
		}
		
		
		/// <summary>
		/// Retorna uma lista de objetos por parametro 
		/// </summary>
		public List<OBJ_TB_ORDER_BOOK_TRUCK>SelecionarLista()
		{						
			return 	DAL.SelecionarLista();
		}
		
		public void PreencheControle(System.Web.UI.Control controle, bool selecione)
        {
            if (controle is DropDownList)
            {
                DropDownList ddl = (DropDownList)controle;
                ddl.Items.Clear();
                foreach (OBJ_TB_ORDER_BOOK_TRUCK obj in DAL.SelecionarLista())
                {
                    ddl.Items.Add(new ListItem(obj.lcd.ToString(), obj.id.ToString()));
                }
                
                if (selecione)
                {
                    ddl.Items.Insert(0, new ListItem("Selecione", ""));
                    ddl.SelectedIndex = 0;
                }

                ddl.DataBind();
            }


            if (controle is ListBox)
            {
                ListBox lst = (ListBox)controle;
                lst.Items.Clear();
                foreach (OBJ_TB_ORDER_BOOK_TRUCK obj in DAL.SelecionarLista())
                {
                    lst.Items.Add(new ListItem(obj.lcd.ToString(), obj.id.ToString()));
                }

                if (selecione)
                {
                    lst.Items.Insert(0, new ListItem("Selecione", ""));
                    lst.SelectedIndex = 0;
                }

                lst.DataBind();
            }

        }										
			
		
		public DataSet ListaCampos()
		{
			 return DAL.ListaCampos();
		}

        public void Importar(FileInfo fi)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            try
            {
                ds = publico.PUB_EXCEL.ImportExcelXLS(fi.FullName, true);

                List<OBJ_TB_ORDER_BOOK_TRUCK> lista_import = new List<OBJ_TB_ORDER_BOOK_TRUCK>();
                dt = ds.Tables[0];


                // PERCORRO O DATA TABLE E PREENCHO A LISTA DE TRUCK
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    OBJ_TB_ORDER_BOOK_TRUCK obj = new OBJ_TB_ORDER_BOOK_TRUCK();

                    obj.lcd = PUB_FUNCOES.TrataData( dt.Rows[i][0].ToString());
                    obj.eol = PUB_FUNCOES.TrataData(dt.Rows[i][1].ToString());
                    obj.cdd = PUB_FUNCOES.TrataData(dt.Rows[i][2].ToString());
                    obj.mercado = Convert.ToInt32(PUB_FUNCOES.TrataInteiro(dt.Rows[i][3].ToString()));
                    obj.fo = Convert.ToInt32(PUB_FUNCOES.TrataInteiro(dt.Rows[i][4].ToString()));
                    obj.green_ok = PUB_FUNCOES.TrataData(dt.Rows[i][5].ToString());
                    obj.online = PUB_FUNCOES.TrataData(dt.Rows[i][6].ToString());
                    obj.asweek = Convert.ToInt32(PUB_FUNCOES.TrataInteiro(dt.Rows[i][7].ToString()));
                    obj.chassi = Convert.ToInt32(PUB_FUNCOES.TrataInteiro(dt.Rows[i][8].ToString()));
                    obj.modalidade = dt.Rows[i][9].ToString();
                    obj.cabine = dt.Rows[i][10].ToString();
                    obj.cor = dt.Rows[i][11].ToString();
                    obj.pacote = dt.Rows[i][12].ToString();
                    obj.eixo = dt.Rows[i][13].ToString();
                    obj.entre_eixo = dt.Rows[i][14].ToString();
                    obj.caixa = dt.Rows[i][15].ToString();
                    obj.tanque_esquerdo = dt.Rows[i][16].ToString();
                    obj.tanque_direito = dt.Rows[i][17].ToString();
                    obj.suspensao = dt.Rows[i][18].ToString();
                    obj.tomada_forca = dt.Rows[i][19].ToString();
                    obj.ptofly = dt.Rows[i][20].ToString();
                    obj.climatizador = dt.Rows[i][21].ToString();
                    obj.ar_condicionado = dt.Rows[i][22].ToString();
                    obj.eixo_traseiro = dt.Rows[i][23].ToString();
                    obj.freio = dt.Rows[i][24].ToString();
                    obj.abs = dt.Rows[i][25].ToString();
                    obj.pneu = dt.Rows[i][26].ToString();
                    obj.modelo_pneu = dt.Rows[i][27].ToString();
                    obj.cliente_original = dt.Rows[i][27].ToString();
                    obj.prom_entrada = dt.Rows[i][28].ToString();
                    obj.cliente_final = dt.Rows[i][29].ToString();
                    obj.cnpj = dt.Rows[i][30].ToString();
                    obj.status = dt.Rows[i][31].ToString();
                    obj.segmento = dt.Rows[i][32].ToString();
                    obj.filial = dt.Rows[i][33].ToString();
                    obj.consultor = dt.Rows[i][34].ToString();
                    obj.valor = dt.Rows[i][35].ToString();
                    obj.comissao = dt.Rows[i][36].ToString();
                    obj.modalidade = dt.Rows[i][37].ToString();
                    obj.financeira = dt.Rows[i][38].ToString();
                    obj.previsao_fat = PUB_FUNCOES.TrataData(dt.Rows[i][39].ToString());
                    obj.pendencia = dt.Rows[i][40].ToString();
                    obj.observacao = dt.Rows[i][41].ToString();
                    obj.cred_aprovado_banco = dt.Rows[i][42].ToString();
                    obj.dt_prot_banco_bnds = PUB_FUNCOES.TrataData( dt.Rows[i][43].ToString());
                    obj.autorizacao_fat = dt.Rows[i][44].ToString();
                    obj.pac = dt.Rows[i][45].ToString();
                    obj.id_usuario = PUB_SESSAO.Id;
                    obj.dt_importacao = DateTime.Now;

                    lista_import.Add(obj);

                }

                // SELECIONO A LISTA DO BANCO DE DADOS
                List<OBJ_TB_ORDER_BOOK_TRUCK> lista_truck = DAL.SelecionarLista();
         

                if (lista_truck.Count > 0)
                {

                    for (int j = 0; j <= lista_import.Count - 1; j++)
                    {
                        OBJ_TB_ORDER_BOOK_TRUCK o_tb_order_book_import = new OBJ_TB_ORDER_BOOK_TRUCK();

                        o_tb_order_book_import = lista_truck.Find((truck) => truck.fo == lista_import[j].fo);

                        if (Equals(o_tb_order_book_import, null))
                        {
                            DAL.Incluir(lista_import[j]);
                        }

                        else
                        {
                            DAL.Alterar(lista_import[j]);
                        }


                    }


                }
                else
                {
                    for (int i = 0; i <= lista_import.Count - 1; i++)
                    {
                        DAL.Incluir(lista_import[i]);
                    }

                }




            }
            catch (Exception)
            {

                throw;
            }

        }
					
		#endregion
	}
	#endregion
}

